package com.njbdqn.myfilm.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.njbdqn.myfilm.componses.PageParam;
import com.njbdqn.myfilm.services.DisplayService;
import com.njbdqn.myfilm.vo.Display;

@Controller
public class InitAction {
	@Autowired
	private DisplayService displayService;
	
	@InitBinder
	public void dateBinder(ServletRequestDataBinder bind){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(true);
		bind.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
	}
	
	@RequestMapping("/init.do")
	public String initAction(HttpServletRequest request,PageParam pip){
		request.setAttribute("pb", displayService.getBean(pip));
		return "index";
	}
	
	@RequestMapping("/all.do")
	public String allAction(HttpServletRequest request){
		request.setAttribute("actor", displayService.findAllActor());
		request.setAttribute("film", displayService.findAllFilm());
		return "online";
	}
	
	@RequestMapping("/save.do")
	public String saveAction(List<Display> displays){
		displayService.saveFilm(displays);
		return "redirect:/init.do";
	}
}
